package com.hisan.freeride.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseAppLication;
import com.hisan.freeride.common.base.BaseFragment;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.utils.MyHandlerUtil;
import com.hisan.freeride.databinding.WelfareBinding;
import com.hisan.freeride.home.activity.LoginActivity;
import com.hisan.freeride.home.activity.LoveCartDetailsActivity;
import com.hisan.freeride.home.activity.WelfareMessageActivity;
import com.hisan.freeride.home.adapter.WelfareAdapter;
import com.hisan.freeride.home.model.WelfareModel;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间 : 2017/6/10
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：公益广场页面
 */
public class WelfareFragment extends BaseFragment<WelfareBinding> {
    private WelfareAdapter welfareAdapter;
    private int page=1;
    private List<WelfareModel> welfareModels=new ArrayList<>();

    //统一的Fragment构建方法
    public static WelfareFragment newInstance() {
        WelfareFragment fragment=null;
        if (fragment==null)fragment= new WelfareFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.welfare;
    }

    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        welfareAdapter=new WelfareAdapter(new ArrayList<>(0),getActivity());
        mBinding.rv.setAdapter(welfareAdapter);
        mBinding.rv.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void initEvent() {
        welfareAdapter.setButtonClickListener((type, data) -> {
            if (!CollectionUtils.isNullOrEmpty(data)){
                if (type==0){
                    getzan(data.getId(),type);
                }else if (type==1){
                        getzan(data.getId(),type);
                }else if (type==2){
                    Bundle bundle=new Bundle();
                    bundle.putInt("driver_id",data.getDriver_id());
                    startActivityForResult(LoveCartDetailsActivity.class,0,bundle,true);
                }else if (type==3){
                    Bundle bundle=new Bundle();
                    bundle.putInt("driver_id",data.getDriver_id());
                    startActivityForResult(LoveCartDetailsActivity.class,0,bundle,true);
                }else if (type==4){
               startActivityForResult(LoginActivity.class,0,null,true);
                }
            }
        });
        welfareAdapter.setOnItemClickListener(data -> {
            if (!CollectionUtils.isNullOrEmpty(data)){
                int id=data.getId();
                Bundle budle=new Bundle();
                budle.putInt("id",id);
                budle.putSerializable("data",data);
                startActivityForResult(WelfareMessageActivity.class,0,budle,true);
            }
        });
        mBinding.pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                page=1;
                welfareModels.clear();
                getdata();
                mBinding.pull.finishRefresh();
            }

            @Override
            public void loadMore() {
                page++;
                getdata();
                mBinding.pull.finishLoadMore();
            }
        });
    }

    @Override
    protected void loadData(boolean isRefresh) {
        welfareModels.clear();
        getdata();
    }

    private void getdata(){
            //可能需要移除之前添加的布局
            welfareAdapter.removeAllFooterView();
            OkGo.<List<WelfareModel>>get(AppConfig.Plaza)
                    .params("city_id", BaseAppLication.getInstance().getCityid())
                    .params("page",page)
                    .params("size",20)
                    .execute(new DialogCallback<List<WelfareModel>>(getActivity()) {
                        @Override
                        public void onSuccess(com.lzy.okgo.model.Response<List<WelfareModel>> response) {
                            super.onSuccess(response);
                            if (!CollectionUtils.isNullOrEmpty(response.body())){
                                if(response.body().size()>=20){
                                    mBinding.pull.setCanLoadMore(true);
                                }else {
                                    mBinding.pull.setCanLoadMore(false);
                                }
                                if (isOk(response)){
                                    welfareModels.addAll(response.body());
                                    showData();
                                    welfareAdapter.setNewData(welfareModels);
                                }else {
                                    showErrorView(response.code());
                                }
                            }else {
                                if (!CollectionUtils.isNullOrEmpty(welfareModels)) welfareAdapter.setNewData(response.body());
                                else noData();
                            }
                        }

                        @Override
                        public void onError(com.lzy.okgo.model.Response<List<WelfareModel>> response) {
                            super.onError(response);
                            showErrorView(response.code());
                        }
                    });

    }
    //点赞
    public void getzan(int id,int type) {
        OkGo.post(AppConfig.Getzan)
                .params("id",id)
                .execute(new DialogCallback<Object>(getActivity()) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response)){
                            if (isOk(response)){
                                welfareModels.clear();
                                page=1;
                                if (type==0){
                                    showToast("取消成功");
                                }else {
                                    showToast("点赞成功");
                                }
                            }else {
                                showError(response);
                            }


                            MyHandlerUtil.getHandler().postDelayed(() -> loadData(true),200);
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        loadData(true);
    }
}
